package com.zretc.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zretc.service.AdminService;
import com.zretc.service.impl.AdminServiceImpl;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
      private AdminService adminService = new AdminServiceImpl(); 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String op = request.getParameter("op");
		if("login_register".equals(op));
		doLogin_register(request,response);
	}

	

	private void doLogin_register(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 调度器
		//调用业务层逻辑
		String admin_name = request.getParameter("admin_name");
		String admin_pwd = request.getParameter("admin_pwd");
		Map<String, String> adminMap = adminService.login(admin_name, admin_pwd);
		if (adminMap != null) {
			//登录成功
			//用户信息存储在session会话中
			HttpSession session = request.getSession();
			session.setAttribute("adminMap", adminMap);
			//重定向
			response.sendRedirect("back/adminHome.jsp");
		}else {
			//登录失败
			//重定向到登录页
			response.sendRedirect("back/login_register.jsp?login_registerErrorMessage=error");
		}
	}

}
